<template>
 <div class="bind-wechat-mobile" v-loading="loading">
   <span v-if="loading">请稍后...</span>
   <span v-else>{{bindStatus}}</span>
 </div>
</template>

<script lang="ts" setup>
/**
 * 账号绑定微信 的回调页面
 */
import vconsole from "vconsole";
import {bindWechat} from "@/api/orgStaff";

const route = useRoute()

const loading = ref(false)
const bindStatus = ref('')
const _bindWechat = async () => {
  loading.value = true
  const tag = route.query?.tag as string ?? ''
  const wxCode = route.query?.code as string ?? ''
  const userId = route.query?.userId as string ?? ''
  const rs = await bindWechat({
    wxCode,
    userId,
    tag,
    app: 'merchant'
  })
  loading.value = false

  if (rs.code === 200) {
    bindStatus.value = '绑定成功'
  } else {
    bindStatus.value = rs?.errorMsgDesc ?? ''
  }
}


const getQuery = () => {
  return new Promise<void>((resolve) => {
    const authUrlQueryString = (location.href.match(/\?.+(?=#)/i)?.[0] ?? '').replace('?', '')
    const newHref = location.href.replace('?' + authUrlQueryString, '')

    if (authUrlQueryString) {
      location.replace(newHref + '&' + authUrlQueryString)
    } else {
      resolve()
    }
  })

}


onMounted(async () => {
  new vconsole()
  getQuery().then(() => {
    _bindWechat()
  })
})
</script>

<style scoped lang="scss">
.bind-wechat-mobile {
  padding: 10px;
}
</style>
